home *** CD-ROM | disk | FTP | other *** search
- Newsgroups: comp.lang.c
- Path: news.uunet.ca!wildcan!sq!msb
- From: msb@sq.com (Mark Brader)
- Subject: Re: problem: sun4 and hpux char * difference
- Message-ID: <1996Apr10.231505.27218@sq.com>
- Summary: Undefined behavior; null pointers are not null strings
- Organization: SoftQuad Inc., Toronto, Canada
- References: <Dpnpn5.Lo3@Cadence.COM>
- Date: Wed, 10 Apr 1996 23:15:05 GMT
-
- [Posted and emailed.]
-
- Timothy McCoy (tmccoy@nntp.cadence.com) writes:
- > I have a program that is made for hpux and sun4 users.
- > The sun program fails and the hpux version does not.
-
- The program's behavior varies because it is undefined. If undefined
- behavior is a difficult concept for you, please reread the discussion
- of it in the FAQ list, particularly the last few questions in Section 11.
-
- > I believe I understand why its dieing on the sun, as
- > a null (zero) address can't ...
-
- Whoa there. Just a moment for a side issue. A null pointer is not
- necessarily represented as a zero address, and hence initializing a
- char * value by calloc()ing the variable does not necessarily yield a
- null pointer. It happens to yield one on common implementations, but
- you can't assume it always will. Please reread the discussion of null
- pointers in Section 5 of the FAQ list. If you have a malloc()ed or
- calloc()ed structure and you want to set one of its members to be a
- null pointer, you should do it explicitly:
-
- lrec->node_name = NULL;
-
- > ...a null (zero) address can't be read from (really) but
- > myself and others thought that it was 'standard' to
- > interpret it as zero and (in this example) copy '\0'
- > into astring, which is just what the hp does.
-
- "Yourself and others" are mistaken. The behavior when you indirect
- through a null pointer is undefined. Some implementations happen
- to behave in the way you described, making a null pointer look like
- a legitimate pointer to a null string; but unless the program is
- intended to run only on such implementations, it has a bug (as you
- found out). Null strings and null pointers simply are two different
- things, as mentioned in question 5.13.
-
- > Additionally, why, Why, WHY does the sun version of this:
- > printf("name='%s'\n", lrec->node_name);
- > yield this:
- > name='(null)'
-
- This feature is provided precisely for the purpose of making it easier
- for you to find and eliminate this bug. If it printed name='', then you
- might easily think that you *really had* a null string, and go off looking
- for some other reason why the program was aborting.
-
-
- > P.S. I resolved a small portion of the problem witha declaration:
- > char *CNULL = { "\0" };
- > ...
- > lrec->node_name = CNULL;
-
- That's one way to fix the code. Most people would eliminate the variable
- CNULL, though. The only significant reason to use it is if you need all
- the node_names that are null strings to also be equal when compared as
- pointers. If you don't need that, then you can just write:
-
- lrec->node_name = "";
-
- (You get a \0 for free at the end of a string literal, so you don't need
- to supply an extra one.) Two node_names separately initialized to ""
- may or may not compare equal as pointers, depending on the implementation.
- (If they are initialized from the same "" literal, they will.)
-
-
- An alternative fix is to let the node_name be a null pointer when there
- is no node_name, and fix the code that dereferences it, by testing whether
- lrec->node_name is a null pointer at the appropriate times. This way
- probably produces cleaner code in more cases than using null strings.
-
-
- --
- Mark Brader, msb@sq.com, SoftQuad Inc., Toronto
- The precedence don't enter into it -- it's stone undefined.
- This expression makes no sense. It has ceased to be. It's
- expired and gone, though sadly not forgotten. This is a latent
- expression. Bereft of meaning, it should rest in peace. If
- people didn't keep nailing it into these discussions, it would be
- pushing up the daisies. It's rung down the curtain and joined
- the choir ineffable. This is not an ex-pression.
- -- Steve Summit (after Monty Python)
- My text in this article is in the public domain.
-